package view;

import Common.Message;
import Common.MessageType;
import Common.User;
import serve.*;

import java.awt.*;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

/*
客户端页面
 */
public class QQCline {
    public boolean loop=true;
    public String key;
    UserClientServe userClientServe=new UserClientServe();
    MessageClientService messageClientService=new MessageClientService();
    FileClientServe fileClientServe=new FileClientServe();
    private void mainMenu(){
        while (loop){
            System.out.println("---------通信页面--------");
            System.out.println("1.登录");
            System.out.println("9.退出");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入选择");
            key=sc.next();
            switch (key){
                case "1":
                    System.out.println("请输入用户编号");
                    String userId=sc.next();
                    System.out.println("请输入用户密码");
                    String password=sc.next();
                    //组装用户对象
                    User user=new User(userId,password);

                    if(userClientServe.checkUser(user)){//判断是否登陆成功
                        System.out.println("----欢迎"+userId+"----");
                        while (loop){
                            System.out.println("----欢迎"+userId+"登录----");
                            System.out.println("1.显示用户列表");
                            System.out.println("2.群发");
                            System.out.println("3.私聊");
                            System.out.println("4.发送文件");
                            System.out.println("9.退出");
                            System.out.println("请输入你的选择");
                            key=sc.next();
                            switch (key){
                                case "1":
                                    userClientServe.onlineFriends(user);
                                    break;
                                case "2":
                                    System.out.println("你想说的话");
                                    String contentAll=sc.next();
                                    messageClientService.sendAll(contentAll,userId);
                                    break;
                                case "3":
                                   //私聊
                                    System.out.println("你想聊天的用户");
                                    String friendId=sc.next();
                                    System.out.println("你想说的话");
                                    String content=sc.next();
                                    messageClientService.sendMessageToOne(content,userId,friendId);

                                    break;
                                case "4":
                                    System.out.println("你想聊天的用户");
                                    String getterId=sc.next();
                                    System.out.println("你文件的位置");
                                    String src=sc.next();
                                    System.out.println("你文件发送哪儿");
                                    String dest=sc.next();
                                  fileClientServe.sendFile(src,userId,getterId,dest);
                                    break;
                                case "9":
                                    logout(user);
                                    loop=false;
                        }

                        }

                    }else {
                        System.out.println("登录失败");
                        // 这里登录失败就直接退出系统了
                    }
                    break;//这里如果没有break 因为穿透特性 执行了 case 为 9 里面的代码程序就退出了
                case "9":
                    System.out.println("退出系统。。。。");
                    loop=false;
                    break;
            }
        }
        System.out.println("你已经退出---");
    }

    public static void main(String[] args) {
        new QQCline().mainMenu();
    }
    public void logout(User user){
        //发送消息
        Message message=new Message();
        message.setMsgType(MessageType.MESSAGE_CLIENT_EXIT);
        //指定退出的人
        message.setSender(user.getUserId());
        ClientConnectServiceThread thread = ManageClientConnectServeThread.getClientConnectServiceThread(user.getUserId());
        Socket socket = thread.getSocket();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
            System.exit(0);
        }catch (Exception e){
            e.getMessage();
        }


    }
}
